package cn.icanci.ladybird.service.admin.impl;

import cn.icanci.ladybird.common.result.JsonData;
import cn.icanci.ladybird.pojo.bo.AdminUpdateBO;
import cn.icanci.ladybird.pojo.entity.Admin;
import cn.icanci.ladybird.service.AdminService;
import cn.icanci.ladybird.service.admin.AdminUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author: icanci
 * @date: Created in 2020/10/9 15:58
 * @classAction: 管理员更新实现类
 */
@Service
public class AdminUpdateServiceImpl implements AdminUpdateService {
    @Autowired
    private AdminService adminService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public JsonData adminUpdate(AdminUpdateBO adminUpdateBO) {
        try {
            Admin admin = new Admin();
            admin.setAdminId(adminUpdateBO.getAdminId());
            admin.setAdminEnabled(adminUpdateBO.getAdminEnabled());
            admin.setAdminUsername(adminUpdateBO.getAdminUsername());
            admin.setAuthority(adminUpdateBO.getAuthority());
            admin.setUpdateTime(new Date());
            adminService.update(admin);
        } catch (Exception e) {
            return JsonData.builderError(e.getLocalizedMessage());
        }
        return JsonData.builderSuccess();
    }
}
